package com.movitech.mobile.cz.base.sys.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.plugins.Page;
import com.movitech.mobile.cz.base.common.util.ListObjCopyUtil;
import com.movitech.mobile.cz.base.modules.config.ArticleTypeEnum;
import com.movitech.mobile.cz.base.modules.config.AttachmentTypeEnum;
import com.movitech.mobile.cz.base.supplier.bizvo.SArticleResponseVO;
import com.movitech.mobile.cz.base.sys.bizvo.AttachmentVO;
import com.movitech.mobile.cz.base.sys.mapper.ext.ExtSArticleMapper;
import com.movitech.mobile.cz.base.util.UserUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.common.util.IdGenUtils;
import com.movitech.mobile.cz.base.common.utils.StringUtils;
import com.movitech.mobile.cz.base.modules.controller.RestReturnResult;
import com.movitech.mobile.cz.base.sys.bizvo.ArticleRequestVO;
import com.movitech.mobile.cz.base.sys.bizvo.ArticleResponseVO;
import com.movitech.mobile.cz.base.sys.entity.SArticle;
import com.movitech.mobile.cz.base.sys.entity.SAttachment;
import com.movitech.mobile.cz.base.sys.mapper.SArticleMapper;
import com.movitech.mobile.cz.base.sys.service.SArticleService;
import com.movitech.mobile.cz.base.sys.service.SAttachmentService;

/**
 * <p>
 * 文章 服务实现类
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-02-28
 */
@Service("sArticleService")
@Transactional
public class SArticleServiceImpl extends ServiceImpl<SArticleMapper, SArticle> implements SArticleService {
	
	@Autowired
	private SAttachmentService sAttachmentService;
	@Autowired
	private ExtSArticleMapper extSArticleMapper;

    @Override
	public RestReturnResult<Object> getSArticleByType(Integer articleType) {
		RestReturnResult<Object> hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);
		try {
		EntityWrapper wrapper = new EntityWrapper<>();
		SArticle sArticle=new SArticle();
		sArticle.setArticleType(articleType);
		sArticle.setDelFlag(CommonConstants.DEL_FLAG_NORMAL);
		wrapper.setEntity(sArticle);
		SArticle sArticleResult= extSArticleMapper.getLastestOneByType(2);
		ArticleResponseVO articleResponseVO=new ArticleResponseVO();
			BeanUtils.copyProperties(sArticleResult,articleResponseVO);
			hResult = new RestReturnResult<Object>(CommonConstants.SUCCESS, articleResponseVO);
		} catch (Exception e) {
			e.printStackTrace();
			hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);
		}
		return hResult;
	}

	@Override
	public RestReturnResult<Object> saveOrUpdateArticle(ArticleRequestVO articleRequestVO) {
		RestReturnResult<Object> hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);
		
		SArticle article = null;
		
		if(articleRequestVO != null){
			
			article = new SArticle();
			article.setArticleType(articleRequestVO.getArticleType());
			article.setArticleTitle(articleRequestVO.getArticleTitle());
			
			Date createDate = new Date();
			String userId = UserUtils.getCurrentUserId();
			article.setId(IdGenUtils.uuid());
			article.setCreateBy(userId);
			article.setCreateDate(createDate);
			article.setUpdateBy(userId);
			article.setUpdateDate(createDate);
			article.setDelFlag(CommonConstants.NO_INTEGER);
			
			baseMapper.insert(article);
			
			hResult = new RestReturnResult<Object>(CommonConstants.SUCCESS, CommonConstants.SUCCESS_CODES[0], CommonConstants.SUCCESS_DESCS[0]);
			
		}else{//请求数据出错
			hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[3], CommonConstants.ERROR_DESCS[3]);
		}
		
		return hResult;
	}

	@Override
	public RestReturnResult<Object> updateArticleForDel(ArticleRequestVO articleRequestVO) {
		RestReturnResult<Object> hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);
		
		if(StringUtils.isNotEmpty(articleRequestVO.getId())){
			
			SArticle article = baseMapper.selectById(articleRequestVO.getId());
			
			if(article != null){
				Date updateDate = new Date();
				String userId = UserUtils.getCurrentUserId();
				
				EntityWrapper wrapper = new EntityWrapper<>();
				SAttachment attachmentParam = new SAttachment();
				attachmentParam.setRelatedId(articleRequestVO.getId());
				attachmentParam.setAttachmentType(articleRequestVO.getArticleType());
		        wrapper.setEntity(attachmentParam);
				
				List<SAttachment> attachmentList = sAttachmentService.selectList(wrapper);
				
				if(attachmentList != null && attachmentList.size() > 0){
					SAttachment attachment = attachmentList.get(0);
					
					attachment.setDelFlag(CommonConstants.YES_INTEGER);
					
					attachment.setUpdateBy(userId);
					attachment.setUpdateDate(updateDate);
					
					sAttachmentService.updateById(attachment);
				}
				
				
				//文章删除
				article.setDelFlag(CommonConstants.YES_INTEGER);
				
				article.setUpdateBy(userId);
				article.setUpdateDate(updateDate);
				
				baseMapper.updateById(article);
				
				hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.SUCCESS_CODES[2], CommonConstants.SUCCESS_DESCS[2]);
			}else{//数据不存在
				hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[2], CommonConstants.ERROR_DESCS[2]);
			}
			
		}else{
			hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[3], CommonConstants.ERROR_DESCS[3]);
		}
		
		return hResult;
	}

	@Override
	public RestReturnResult<Object> getHelpCenter(ArticleRequestVO articleRequestVO) throws Exception {
		RestReturnResult<Object> hResult = new RestReturnResult<Object>(CommonConstants.FAIL,articleRequestVO, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);
		if(articleRequestVO.getPageFlag()!=null&&
				articleRequestVO.getPageFlag()==CommonConstants.YES_INTEGER){
			Page<SArticle> page = new Page<>(articleRequestVO.getPageNumber(),articleRequestVO.getPageSize(),"update_date",false);
			EntityWrapper wrapper = new EntityWrapper<>();
			SArticle sArticle = new SArticle();
			sArticle.setArticleType(ArticleTypeEnum.articleType1.getCode());
			wrapper.setEntity(sArticle);
			page = this.selectPage(page,wrapper);
			Page<SArticleResponseVO> sArticleResponseVOPage= new Page<SArticleResponseVO>();
			List<SArticleResponseVO> sArticleResponseVOList = ListObjCopyUtil.copyTo(page.getRecords(), SArticleResponseVO.class);
			for(SArticleResponseVO obj:sArticleResponseVOList){
				EntityWrapper wrapperAttachment = new EntityWrapper<>();
				SAttachment attachmentParam = new SAttachment();
				attachmentParam.setRelatedId(obj.getId());
				attachmentParam.setAttachmentType(AttachmentTypeEnum.ATTACHMENT_TYPE_37.getCode());
				attachmentParam.setDelFlag(CommonConstants.DEL_FLAG_NORMAL);
				wrapperAttachment.setEntity(attachmentParam);
				List<SAttachment> attachmentList = sAttachmentService.selectList(wrapperAttachment);
				if(attachmentList!=null&&attachmentList.size()>0){
					obj.setLsAttachmentVO(ListObjCopyUtil.copyTo(attachmentList, AttachmentVO.class));
				}
			}
			sArticleResponseVOPage.setRecords(sArticleResponseVOList);
			sArticleResponseVOPage.setTotal(page.getTotal());
			sArticleResponseVOPage.setSize(page.getSize());
			sArticleResponseVOPage.setCurrent(page.getCurrent());
			hResult=RestReturnResult.success();
			hResult.setData(sArticleResponseVOPage);
		}else{
			EntityWrapper wrapper = new EntityWrapper<>();
			SArticle sArticle = new SArticle();
			sArticle.setDelFlag(CommonConstants.DEL_FLAG_NORMAL);
			sArticle.setArticleType(ArticleTypeEnum.articleType1.getCode());
			List<SArticle> ls=this.selectList(wrapper);
			List<SArticleResponseVO> lsSArticleResponseVO=new ArrayList<>();
			if(ls!=null&&ls.size()>0){
				lsSArticleResponseVO=ListObjCopyUtil.copyTo(ls,SArticleResponseVO.class);
				for(SArticleResponseVO obj:lsSArticleResponseVO){
					EntityWrapper wrapperAttachment = new EntityWrapper<>();
					SAttachment attachmentParam = new SAttachment();
					attachmentParam.setRelatedId(obj.getId());
					attachmentParam.setAttachmentType(AttachmentTypeEnum.ATTACHMENT_TYPE_37.getCode());
					attachmentParam.setDelFlag(CommonConstants.DEL_FLAG_NORMAL);
					wrapperAttachment.setEntity(attachmentParam);
					List<SAttachment> attachmentList = sAttachmentService.selectList(wrapperAttachment);
					if(attachmentList!=null&&attachmentList.size()>0){
						obj.setLsAttachmentVO(ListObjCopyUtil.copyTo(attachmentList, AttachmentVO.class));
					}
				}
			}
			hResult=RestReturnResult.success();
			hResult.setData(lsSArticleResponseVO);
		}
		return hResult;
	}

}
